function wxpay(app, oid, typeid = 0, relaunchUrl = '', receiveCard = 0) {
  if(relaunchUrl == null || relaunchUrl == ''){
    relaunchUrl = '/pages/home/index?isStartup=2';
  }
  
  wx.request({
    url: app.globalData.host + '/api/wechat/pay',
    data: {
      uid: app.globalData.userInfo.Uid,
      storeId: app.globalData.storeId,
      oid: oid,
      receiveCard: receiveCard
    },
    success: function (res) {
      if (res.data.Code == 0) {
        if (res.data.Data.trade_state == "SUCCESS") {
          if (typeid == 0) {
            wx.showToast({title: '创建成功',mask: true});  //不用支付
            getBranch(app);
          }
          if (typeid == 2) {
            wx.showToast({title: '支付成功',mask: true})
            setTimeout(function () {
              wx.reLaunch({url: relaunchUrl})
            }, 1000)
          }
          //免费版推广版
          if(typeid == 3){
            setTimeout(function () {
              wx.reLaunch({url: relaunchUrl})
            }, 1000)
          }
        } else {
          wx.requestPayment({
            timeStamp: res.data.Data.timeStamp,
            nonceStr: res.data.Data.nonceStr,
            package: 'prepay_id=' + res.data.Data.prepayId,
            signType: 'MD5',
            paySign: res.data.Data.paySign,
            success: function () {
              wx.showToast({title: '支付成功'});
              if (typeid == 1) {return;} 
              if (typeid == 2) {
                wx.reLaunch({url: relaunchUrl});
                return;
              }
              getBranch(app);
            },
            fail: function (err) {
              if (typeid == 2) {
                wx.reLaunch({url: relaunchUrl})
              }
            }
          })
        }
      } else {
        wx.showModal({
          title: '提示',
          content: res.data.Message,
          showCancel: false,
        })
        return;
      }
    }
  })
}

// 外卖支付
function wxpayWm(app, oid, relaunchUrl, receiveCard = 0) {
  console.log('外卖支付=======');
  wx.request({
    url: app.globalData.host + '/api/wechat/JJKOpenPay',
    data: {
      uid: app.globalData.userInfo.Uid,
      storeId: app.globalData.storeId,
      oid: oid,
      receiveCard: receiveCard
    },
    success: function (res) {
      if (res.data.Code == 0) {
        if (res.data.Data.trade_state == "SUCCESS") {//不用支付
          setTimeout(function () {
            wx.reLaunch({url: relaunchUrl})
          }, 1000)
        } else {
          // 发起支付
          wx.requestPayment({
            timeStamp: res.data.Data.timeStamp,
            nonceStr: res.data.Data.nonceStr,
            package: 'prepay_id=' + res.data.Data.prepayId,
            signType: 'MD5',
            paySign: res.data.Data.paySign,
            fail: function (err) {
              setTimeout(function () {
                wx.reLaunch({url: relaunchUrl})
              }, 500);
              wx.showToast({title: '付款失败',duration: 500})
            },
            success: function (res) {            
              wx.showToast({title: '支付成功',duration: 500});
              setTimeout(function () {
                wx.reLaunch({url: relaunchUrl})
              }, 500);
              
            }
          })
        }

      } else {
        wx.showModal({
          title: '提示',
          content: res.data.Message,
          showCancel: false,
        })
      }
    }
  })
}

// 卖券返佣支付
function wxpayCoupon(app, oid, saleUid, relaunchUrl) {
  wx.request({
    url: app.globalData.host + '/api/wechat/CouponPay',
    data: {
      uid: app.globalData.userInfo.Uid,
      storeId: app.globalData.storeId,
      oid: oid,
      saleUid: saleUid
    },
    success: function (res) {
      if (res.data.Code == 0) {
        // 发起支付
        wx.requestPayment({
          timeStamp: res.data.Data.timeStamp,
          nonceStr: res.data.Data.nonceStr,
          package: 'prepay_id=' + res.data.Data.prepayId,
          signType: 'MD5',
          paySign: res.data.Data.paySign,
          fail: function (err) {
            setTimeout(function () {
              wx.reLaunch({
                url: relaunchUrl
              })
            }, 500);
          },
          success: function () {
            wx.showToast({
              title: '支付成功',
              duration: 500
            })
            setTimeout(function () {
              wx.reLaunch({url: relaunchUrl})
            }, 500);
          }
        })
      } else {
        wx.showModal({
          title: '提示',
          content: res.data.Message,
          showCancel: false,
        })
      }
    }
  })
}

// 现金升级支付
function wxcashpay(app, oid, redirectUrl) {
  wx.request({
    url: app.globalData.host + '/api/WeChat/StaffOrderPay',
    data: {
      storeId: app.globalData.storeId,
      oid: oid
    },
    success: function (res) {
      if (res.data.Code == 0) {
        //发起支付
        wx.requestPayment({
          timeStamp: res.data.Data.timeStamp,
          nonceStr: res.data.Data.nonceStr,
          package: 'prepay_id=' + res.data.Data.prepayId,
          signType: 'MD5',
          paySign: res.data.Data.paySign,
          success: function (res) {
            wx.showToast({
              title: '支付成功'
            })
            wx.navigateBack({
              delta: 1,
            })
          },
          fail: function (aaa) {
            wx.showModal({
              title: '提示',
              content: '支付失败',
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  wx.reLaunch({
                    url: '/pages/home/index?isStartup=2',
                  })
                }
              }
            })
            return;
          }
        })
      } else {
        wx.showModal({
          title: '提示',
          content: res.data.Message,
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              wx.redirectTo({
                url: '/pages/h5page/index'
              });
            }
          }
        })
        return;
      }
    }
  })
}

// 9.9产品押金支付/玖玖卡充值
function jiujiupay(app, oid, redirectUrl, uid = '') {
  let param = {
    storeId: app.globalData.storeId,
    oid: oid
  }
  if (uid) {
    param = {
      uid: uid,
      storeId: app.globalData.storeId,
      oid: oid
    }
  }
  wx.request({
    url: app.globalData.host + '/api/WeChat/StaffOrderPay',
    data: param,
    method: 'POST',
    success: function (res) {
      if (res.data.Code == 0) {
        //发起支付
        wx.requestPayment({
          timeStamp: res.data.Data.timeStamp,
          nonceStr: res.data.Data.nonceStr,
          package: 'prepay_id=' + res.data.Data.prepayId,
          signType: 'MD5',
          paySign: res.data.Data.paySign,
          success: function (res) {
            wx.showToast({
              title: '支付成功'
            })
            setTimeout(function () {
              wx.reLaunch({
                url: redirectUrl,
              })
            }, 1000)
          },
        })
      } else {
        wx.showModal({
          title: '提示',
          content: res.data.Message,
          showCancel: false
        })
        return;
      }
    }
  })
}

//新用户创建订单支付
function createStaffOrder(app, shareStaffId, versionid, jump = 0,typeid = 0, relaunchUrl = '') {
  wx.showLoading({title: '正在创建',mask: true})
  wx.request({
    url: app.globalData.host + '/api/Order/SubmitCreateStaffOrder',
    data: {
      uid: app.globalData.userInfo.Uid,
      shareStaffId: shareStaffId,
      industryCode: versionid,
      jump: jump
    },
    method: 'POST',
    success: function (res) {
      wx.hideLoading();
      if (res.data.Code == 0) {
        wxpay(app, res.data.Data,typeid,relaunchUrl);
      } else {
        wx.showModal({
          title: '提示',
          content: res.data.Message,
          showCancel: false,
        })
      }
    },
    fail: function (err) {
      wx.showModal({
        title: '提示',
        content: err.data.Message,
        showCancel: false,
      })
    }
  })
}

function getBranch(app) {
  wx.request({
    url: app.globalData.host + '/api/staff/GetLastBranch?uid=' + app.globalData.userInfo.Uid,
    success: function (res) {
      if (res.data.Code == 0) {
        if (res.data.Data) {
          wx.reLaunch({
            url: '/pages/index/index?id=' + res.data.Data.Id,
          })
        } else {
          getMyStaffIdByUid(app);
        }
      }
    },
  })
}

//获取个人创建的名片
function getMyStaffIdByUid(app) {
  wx.request({
    url: app.globalData.host + '/api/Staff/GetStaffIdByUid?Uid=' + app.globalData.userInfo.Uid,
    success: function (res) {
      if (res.data.Code == 0) {
        if (!res.data.Data) {
        } else {
          wx.setStorageSync("staffid", res.data.Data.Id);
          wx.reLaunch({
            url: '/pages/index/index?id=' + res.data.Data.Id,
          })
        }
      } else {
        wx.showModal({
          title: '提示',
          content: res.data.Message,
          showCancel: false,
        })
      }
    },
    fail: function (err) {
      wx.showModal({
        title: '提示',
        content: err.data.Message,
        showCancel: false,
      })
    }
  })
}

module.exports = {
  wxpay: wxpay,
  wxpayWm: wxpayWm,
  wxpayCoupon: wxpayCoupon,
  wxcashpay: wxcashpay,
  jiujiupay: jiujiupay,
  createStaffOrder: createStaffOrder,
  getStaffIdByUid: getMyStaffIdByUid
}